package com.fourth.controller.api;

import com.fourth.service.EmpInfoService;
import com.fourth.vo.EmpConditionVO;
import com.github.pagehelper.PageInfo;
import com.ljx.entity.EmpInfo;
import com.ljx.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Mr.ma
 * @since 2023/2/27 12:01
 **/
@RestController
@RequestMapping("/emp")
@Api("员工操作接口")
public class ApiEmpInfoController {

    @Resource
    private EmpInfoService empInfoService;

    @RequestMapping("selectAll")
    @ApiOperation("查询员工信息，含分页以及条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "页码", dataType = "int" , defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "int" , defaultValue = "5"),
            @ApiImplicitParam(name = "condition", value = "查询条件", dataType = "EmpConditionVO")
    })
    public Result<PageInfo<EmpInfo>> selectAll(@RequestParam(defaultValue = "1") Integer currentPage,
                                           @RequestParam(defaultValue = "5")Integer pageSize,
                                           @RequestBody EmpConditionVO condition){
        PageInfo<EmpInfo> empInfos = empInfoService.selectAll(currentPage,pageSize,condition);
        return new Result<>(2000,"查询成功",empInfos);
    }

    @RequestMapping("insertEmp")
    @ApiOperation("插入员工信息")
    @ApiImplicitParam(name = "empInfo", value = "员工信息", dataType = "EmpInfo")
    public Result insertEmp(@RequestBody EmpInfo empInfo){
        int row = empInfoService.insert(empInfo);
        if (row > 0){
            return new Result(2000,"插入成功",null);
        }
        return new Result(5000,"插入失败",null);
    }

    @RequestMapping("updateEmp")
    @ApiOperation("修改员工信息")
    @ApiImplicitParam(name = "empInfo", value = "员工信息", dataType = "EmpInfo")
    public Result updateEmp(@RequestBody EmpInfo empInfo){
        int row = empInfoService.updateEmp(empInfo);
        if (row > 0){
            return new Result(2000,"修改成功",null);
        }
        return new Result(5000,"修改失败",null);
    }

    @RequestMapping("deleteById/{id}")
    @ApiOperation("删除员工信息")
    @ApiImplicitParam(name = "id", value = "员工编号", dataType = "String")
    public Result deleteById(@PathVariable String id){
        int row = empInfoService.deleteById(id);
        if (row > 0){
            return new Result(2000,"删除成功",null);
        }
        return new Result(5000,"删除失败",null);
    }
}
